package com.gec.zhs.controller;

import com.gec.zhs.annotation.ZhsLogin;
import com.gec.zhs.entity.dto.TokenUserInfoDto;
import com.gec.zhs.entity.enums.ResponseCodeEnum;
import com.gec.zhs.entity.pojo.UserVideoSeries;
import com.gec.zhs.entity.pojo.UserVideoSeriesVideo;
import com.gec.zhs.entity.pojo.VideoInfo;
import com.gec.zhs.entity.query.UserVideoSeriesQuery;
import com.gec.zhs.entity.query.UserVideoSeriesVideoQuery;
import com.gec.zhs.entity.query.VideoInfoQuery;
import com.gec.zhs.entity.vo.ResponseVO;
import com.gec.zhs.entity.vo.UserVideoSeriesDetailVO;
import com.gec.zhs.exception.BusinessException;
import com.gec.zhs.service.UserVideoSeriesService;
import com.gec.zhs.service.UserVideoSeriesVideoService;
import com.gec.zhs.service.VideoInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhs
 * @version 1.0
 * @date 2025-02-24
 * @description 个人首页 - > 视频列表
 */
@RestController
@RequestMapping("/uhome/series")
public class UserHomeVideoSeriesController extends ABaseController {

    @Autowired
    private VideoInfoService videoInfoService;

    @Autowired
    private UserVideoSeriesService userVideoSeriesService;

    @Autowired
    private UserVideoSeriesVideoService userVideoSeriesVideoService;

    @RequestMapping("/loadVideoSeries")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadVideoSeries(String userId) {
        List<UserVideoSeries> userAllSeries = userVideoSeriesService.getUserAllSeries(userId);
        return getSuccessResponseVO(userAllSeries);
    }

    /**
     * 保存系列
     *
     * @param seriesId
     * @param seriesName
     * @param seriesDescription
     * @param videoIds
     * @return
     */
    @RequestMapping("/saveVideoSeries")
    @ZhsLogin(checkLogin = true)
    public ResponseVO saveVideoSeries(Integer seriesId, String seriesName, String seriesDescription, String videoIds) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserVideoSeries videoSeries = new UserVideoSeries();
        videoSeries.setUserId(tokenUserInfoDto.getUserId());
        videoSeries.setSeriesId(seriesId);
        videoSeries.setSeriesName(seriesName);
        videoSeries.setSeriesDescription(seriesDescription);
        userVideoSeriesService.saveUserVideoSeries(videoSeries, videoIds);
        return getSuccessResponseVO(null);
    }

    /**
     * 展示每个系列和每个系列的第一个视频信息(封面等)
     * SELECT t.*,v.video_cover FROM(
     * 	SELECT *,
     * 	( SELECT video_id FROM user_video_series_video v WHERE v.series_id = s.series_id ORDER BY sort ASC LIMIT 1 ) video_id
     * 	FROM
     * 		user_video_series s
     * 	) t
     * 	LEFT JOIN video_info v ON v.video_id = t.video_id;
     */
    /**
     * 往视频列表中添加视频时需要
     *
     * @param seriesId
     * @return
     */
    @ApiOperation("加载全部的视频")
    @RequestMapping("loadAllVideo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadAllVideo(Integer seriesId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        if (seriesId != null) {
            UserVideoSeriesVideoQuery videoSeriesVideoQuery = new UserVideoSeriesVideoQuery();
            videoSeriesVideoQuery.setSeriesId(seriesId);
            videoSeriesVideoQuery.setUserId(tokenUserInfoDto.getUserId());
            List<UserVideoSeriesVideo> seriesVideoList = userVideoSeriesVideoService.findListByParam(videoSeriesVideoQuery);

            List<String> videoList = seriesVideoList.stream().map(item -> item.getVideoId()).collect(Collectors.toList());
            videoInfoQuery.setExcludeVideoIdArray(videoList.toArray(new String[videoList.size()]));
        }
        videoInfoQuery.setUserId(tokenUserInfoDto.getUserId());
        List<VideoInfo> videoInfoList = videoInfoService.findListByParam(videoInfoQuery);
        return getSuccessResponseVO(videoInfoList);
    }

    /**
     * 个人首页 -> 系列 -> 系列点进去展示视频
     * @param seriesId
     * @return
     */
    @ApiOperation("获取视频详情信息")
    @RequestMapping("/getVideoSeriesDetail")
    @ZhsLogin(checkLogin = true)
    public ResponseVO getVideoSeriesDetail(Integer seriesId) {
        UserVideoSeries videoSeries = userVideoSeriesService.getUserVideoSeriesBySeriesId(seriesId);
        if (videoSeries == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_404);
        }

        UserVideoSeriesVideoQuery videoSeriesVideoQuery = new UserVideoSeriesVideoQuery();
        videoSeriesVideoQuery.setOrderBy("sort asc");
        videoSeriesVideoQuery.setQueryVideoInfo(true);
        videoSeriesVideoQuery.setSeriesId(seriesId);
        List<UserVideoSeriesVideo> seriesVideoList = userVideoSeriesVideoService.findListByParam(videoSeriesVideoQuery);

        return getSuccessResponseVO(new UserVideoSeriesDetailVO(videoSeries, seriesVideoList));
    }

    /**
     * 往系列集合添加视频
     *
     * @param seriesId
     * @param videoIds
     * @return
     */
    @ApiOperation("保存系列中的视频")
    @RequestMapping("/saveSeriesVideo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO saveSeriesVideo(Integer seriesId, String videoIds) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.saveSeriesVideo(tokenUserInfoDto.getUserId(), seriesId, videoIds);
        return getSuccessResponseVO(null);
    }

    /**
     * 删除视频
     *
     * @param seriesId
     * @param videoId
     * @return
     */
    @ApiOperation("删除系列集合中的视频")
    @RequestMapping("/delSeriesVideo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO delSeriesVideo(Integer seriesId, String videoId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.delSeriesVideo(tokenUserInfoDto.getUserId(), seriesId, videoId);
        return getSuccessResponseVO(null);
    }

    /**
     * 删除系列
     *
     * @param seriesId
     * @return
     */
    @ApiOperation("删除系列")
    @RequestMapping("/delVideoSeries")
    @ZhsLogin(checkLogin = true)
    public ResponseVO delVideoSeries(Integer seriesId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.delVideoSeries(tokenUserInfoDto.getUserId(), seriesId);
        return getSuccessResponseVO(null);
    }


    /**
     * 系列排序
     *
     * @param seriesIds
     * @return
     */
    @ApiOperation("系列集合拽动排序")
    @RequestMapping("/changeVideoSeriesSort")
    @ZhsLogin(checkLogin = true)
    public ResponseVO changeVideoSeriesSort(String seriesIds) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userVideoSeriesService.changeVideoSeriesSort(tokenUserInfoDto.getUserId(), seriesIds);
        return getSuccessResponseVO(null);
    }

    /**
     * 个人首页 - > 展示多少个系列 ->每个系列展示5个视频
     * @param userId
     * @return
     */
    @ApiOperation("加载系列及视频")
    @RequestMapping("/loadVideoSeriesWithVideo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadVideoSeriesWithVideo(String userId) {
        UserVideoSeriesQuery userVideoSeriesQuery = new UserVideoSeriesQuery();
        userVideoSeriesQuery.setUserId(userId);
        userVideoSeriesQuery.setOrderBy("sort asc");
        List<UserVideoSeries> listWithVideoList = userVideoSeriesService.findListWithVideoList(userVideoSeriesQuery);
        return getSuccessResponseVO(listWithVideoList);
    }
}
